//
//  BHActivityViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/10/11.
//

import UIKit

class BHActivityViewController: BHBaseViewController {

    lazy var nextBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect(x: 16, y: UIScreen.height-UIScreen.safeBottomHeight-10-44, width: UIScreen.width-32, height: 44)
        btn.backgroundColor = .color_34CE98
        btn.setTitle("立即参与", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
        btn.addTarget(self, action: #selector(nextPublishAct), for: .touchUpInside)
        btn.addLayerCorner(22, 0, .white)
        return btn
    }()
    
    lazy var waterFlowLayout: DPHomeWaterFlowLayout = {
        let layout = DPHomeWaterFlowLayout.init()
        layout.delegate = self
        return layout
    }()
    lazy var collectionView: UICollectionView = {
        let view = UICollectionView.buildCollectionView(layout: self.waterFlowLayout)
        view.backgroundColor = .color_f8f8f8
        view.register(UINib.init(nibName: "BHHomeCircleItemCell", bundle: nil), forCellWithReuseIdentifier: "BHHomeCircleItemCell")
        view.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.showHeight-UIScreen.safeBottomHeight-10-44)
        view.delegate = self
        view.dataSource = self
        
        return view
    }()
    lazy var serviceManager: BHCircleServiceManager = {
        let manager = BHCircleServiceManager.init()
        return manager
    }()
    fileprivate var columnListArray: [BHCircleListModel] = []
    
    var activityName: String = ""
    var columnId: String = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.bh_navigationBar.title_label.text = self.activityName;
        self.responseNetService()
        self.view.addSubview(self.collectionView)
        self.view.addSubview(self.nextBtn)
        self.serviceManager.loadCircleListService(columId: self.columnId, page: 1)
        
        self.collectionView.addRefresh { [weak self] in
            self?.page = 1
            self?.serviceManager.loadCircleListService(columId: "", page: 1)
        }
        self.collectionView.addloadMore { [weak self] in
            self?.page += 1
            self?.serviceManager.loadCircleListService(columId: "", page: self?.page ?? 1)
        }
    }
    

    @objc func nextPublishAct() {
        if self.nextBtn.isSelected == true {
            return
        }
        self.nextBtn.isSelected = true
        BHRouter().bh_goPublishCircle(activityId: self.columnId)
        
        self.nextBtn.isSelected = false
    }
}
extension BHActivityViewController: DPHomeWaterFlowLayoutDelegate {
    
    func heightForRowAtIndexPath(collectionView collection: UICollectionView, layout: DPHomeWaterFlowLayout, indexPath: IndexPath, itemWidth: CGFloat) -> CGFloat {
        if indexPath.row < self.columnListArray.count {
            let model = self.columnListArray[indexPath.row]
            return  BHHomeCircleItemCell.circleHeight+model.textHeight+58
        }else {
            return 0
        }
    }
    
    func columnNumber(collectionView collection: UICollectionView, layout: DPHomeWaterFlowLayout, section: Int) -> Int {
        return 2

    }
    
    func interitemSpacing(collectionView collection: UICollectionView, layout: DPHomeWaterFlowLayout, section: Int) -> CGFloat {
        return 8

    }
    func lineSpacing(collectionView collection: UICollectionView, layout: DPHomeWaterFlowLayout, section: Int) -> CGFloat {
        return 10

    }
    func insetForSection(collectionView collection: UICollectionView, layout: DPHomeWaterFlowLayout, section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 16, bottom: 0, right: 16)
    }
}

extension BHActivityViewController: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.columnListArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.row < self.columnListArray.count {
            let model = self.columnListArray[indexPath.row]
            let halfW = (UIScreen.width-40)/2.0
            return  CGSize(width: halfW-1, height: BHHomeCircleItemCell.circleHeight+model.textHeight+58)
        }else {
            return .zero
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.columnListArray[indexPath.row]
        BHRouter.shared.bh_goCircleDetail(circleId: model.circleId)
    }
}

extension BHActivityViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHHomeCircleItemCell", for: indexPath) as! BHHomeCircleItemCell
        let model = self.columnListArray[indexPath.row]
        cell.circleModel = model
        cell.likeBtn.addBtnAction(action: {[weak self] (_) in
            self?.serviceManager.circleLikeOrNot(model: model)
        }, for: .touchUpInside)
        return cell
    }
    
    
}
extension BHActivityViewController {
    
    func responseNetService()  {
        self.serviceManager.circleColumnListResponseBlock = { [weak self] (success,errorMsg,total,array) in
          
            self?.collectionView.endRefresh()
            self?.collectionView.endLoading()
            if success == true {
                if self?.page == 1 {
                    self?.collectionView.endLoadWithNoMoreData(false)
                    if self?.columnListArray.count != 0 {
                        self?.columnListArray.removeAll()
                    }
                }
                self?.columnListArray.append(contentsOf: array)
                
                if self?.columnListArray.count ?? 0 >= total {
                    self?.collectionView.endLoadWithNoMoreData(true)
                }
                
                DispatchQueue.main.async {
                    self?.collectionView.reloadData()
                }
            }else {
                if self?.page ?? 1 > 1 {
                    self?.page -= 1
                }
            }
            
        }
        
        self.serviceManager.circleLikeOrNotResponseBlock = {[weak self] (success, errorMsg) in
            self?.collectionView.reloadData()
        }
    }
}
